package com.suse.lhw.hms.authserver.tools;

import com.alibaba.fastjson.JSON;
import com.suse.lhw.hms.authserver.POJO.Scopes;
import com.suse.lhw.hms.authserver.constan.SecurityConstant;
import com.suse.lhw.hms.authserver.domin.URole;
import com.suse.lhw.hms.authserver.service.URoleService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang.StringUtils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.authserver.tools
 * @ClassName: TokenUtil
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/9 11:20
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/9 11:20
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Configuration
public class TokenUtil {

    //通过setter注入uRoleService防止循环注入
//    @Autowired
//    @Lazy
//    public void setuRoleService(URoleService uRoleService) {
//        this.uRoleService = uRoleService;
//    }
//    private URoleService uRoleService;
    @Autowired
    private URoleService uRoleService;


    /**
     * @Author:Hongwei Li
     * @Description:生成新的token方法，并将角色保存
     * @Date:13:40 2020/4/9
     * @Param:[authUserDetails]
     * @Return:java.lang.String
     **/
    @Bean
    public String createAccessJwtToken(String username) {
//        if (StringUtils.isBlank(username)) {
//            throw new IllegalArgumentException("用户名为空无法创建token");
//        }
//
//        Claims claims = Jwts.claims().setSubject(username);
//        //存入角色信息
//        List<Integer> list=new ArrayList<>();
//        List<URole> urlist=uRoleService.seleallRolesByUserId(username);
//        for (URole uRole : urlist) {
//            list.add(uRole.getId());
//        }
//        claims.put(SecurityConstant.AUTHORITIES, JSON.toJSONString(list));
//        LocalDateTime currentTime = LocalDateTime.now();
//        String token = Jwts.builder()
//                .setClaims(claims)
//                .setIssuer(SecurityConstant.tokenIssuer)
//                .setIssuedAt(Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant()))
//                .setExpiration(Date.from(currentTime
//                        .plusMinutes(SecurityConstant.tokenExpirationTime)
//                        .atZone(ZoneId.systemDefault()).toInstant()))
//                .signWith(SignatureAlgorithm.HS512, SecurityConstant.tokenSigningKey)
//                .compact();
//
//        return token;
        return null;
    }


    /**
     * @Author:Hongwei Li
     * @Description:创建刷新token
     * @Date:13:39 2020/4/9
     * @Param:[authUserDetails]
     * @Return:java.lang.String
     **/
    @Bean
    public String createRefreshToken(String username) {

//        if (StringUtils.isBlank(username)) {
//            throw new IllegalArgumentException("用户名为空无法创建token");
//        }
//
//        LocalDateTime currentTime = LocalDateTime.now();
//
//        Claims claims = Jwts.claims().setSubject(username);
//        claims.put(SecurityConstant.AUTHORITIES, Arrays.asList(Scopes.REFRESH_TOKEN.authority()));
//
//        String token = Jwts.builder()
//                .setClaims(claims)
//                .setIssuer(SecurityConstant.tokenIssuer)
//                .setId(UUID.randomUUID().toString())
//                .setIssuedAt(Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant()))
//                .setExpiration(Date.from(currentTime
//                        .plusMinutes(SecurityConstant.refreshTokenExpTime)
//                        .atZone(ZoneId.systemDefault()).toInstant()))
//                .signWith(SignatureAlgorithm.HS512, SecurityConstant.tokenSigningKey)
//                .compact();
//        return token;
        return null;
    }
}
